package com.google.code.virtualhockey.vhx.client.vp;

import com.google.code.virtualhockey.vhx.client.JsUtils;
import com.google.code.virtualhockey.vhx.client.model.JsNameValuePair;
import com.google.code.virtualhockey.vhx.client.res.VhxResources;
import com.google.code.virtualhockey.vhx.client.uitools.IView;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Singleton;

/**
 * Represents the application's content area.
 */
@Singleton
public class ContentView extends ResizeComposite implements IView {

  interface Binder extends UiBinder<ScrollPanel, ContentView> {
  }

  private static final Binder BINDER = GWT.create( Binder.class );

  @UiField( provided = true )
  final VhxResources m_res = VhxResources.INSTANCE;

  @UiField( provided = true )
  final CellTable<JsNameValuePair> m_tblSysProps;

  public ContentView(){
    m_tblSysProps = new CellTable<JsNameValuePair>();
    m_tblSysProps.addColumn( new TextColumn<JsNameValuePair>() {
      @Override
      public String getValue( JsNameValuePair pair ) {
        return pair.getName();
      }
    }, "System Property" );
    m_tblSysProps.addColumn( new TextColumn<JsNameValuePair>() {
      @Override
      public String getValue( JsNameValuePair pair ) {
        return pair.getValue();
      }
    }, "Value" );

    initWidget( BINDER.createAndBindUi( this ) );
  }

  @Override
  public Widget getViewWidget() {
    return this;
  }

  void setProperties( JsArray<JsNameValuePair> jsaProps ) {
    m_tblSysProps.setRowCount( jsaProps.length() );
    m_tblSysProps.setRowData( JsUtils.toList( jsaProps ) );
    m_tblSysProps.redraw();
  }
}
